package hyung.jin.seo;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.StringTokenizer;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MatchNumber extends HttpServlet{
	
	private int[] latest; // keep the latest popular numbers
	private int supplement; // keep the latest popular supplement
	private int draw; // keep the latest draw to compare
	private ConnectionManager cm;
	
	public MatchNumber() 
	{
		cm = new ConnectionManager();
		try {
			try {
				cm.connect();
				cm.createTable();
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		latest = new int[7];
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(req, res);
		res.setContentType("text/html;charset=UTF-8");
		PrintWriter out = res.getWriter();
		String action = req.getParameter("action");
		if(action==null || action.equals(""))
		{
			out.println("No action !!!");
			out.close();
			return;
		}
		if(action.equals("statistics"))
		{
			getStatistics(req, out);
		}else if(action.equals("result")){
			getResult(req, out);
		}else{
			out.println("What do you want ??");
			out.close();
		}
	}
	
	private void getResult(HttpServletRequest req, PrintWriter out) 
	{
//		String numbers = req.getParameter("nums");
//		if(numbers==null || numbers.equals(""))
//		{
//			out.println("Nothing comes");
//			out.close();
//		}
//		int[] params = chop(numbers);
//		int draw = getRecentDraw();
//		if(this.draw!=draw)
//		{
//			this.draw = draw;
//			int[] results = getResult();
//			int[] nums = new int[6];
//			// copy only numbers into 'nums' array
//			System.arraycopy(results, 0, nums, 0, nums.length);
//			// sort them before putting into 'latest' array
//			Arrays.sort(nums);
//			latest = nums;
//			supplement = results[results.length-1];
//		}
//		int matchNum = compareNums(params);
//		boolean matchSuppl = compareSupplement(params);
//        out.println("Matching Number is " + matchNum + " : " + matchSuppl);
//		out.close();

		out.println("Get result !!!");
		out.close();
		return;
	}

	private void getStatistics(HttpServletRequest req, PrintWriter out) 
	{
		int[] popular = new int[Constants.NUMBER_BALL+1];
		try {
			popular = cm.getPopularity();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				cm.disconnect();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		out.println(combine(popular));
		out.close();
		return;
	}

	private int[] chop(String parameter) 
	{
		int[] nums = new int[7];
		StringTokenizer st = new StringTokenizer(parameter, "|");
		int cnt = 0;
		while(st.hasMoreTokens())
		{
			try{
				nums[cnt] = Integer.parseInt(st.nextToken().trim());
			}catch(NumberFormatException e){
				nums[cnt] = 0;
			}
			cnt++;
		}
		return nums;
	}
	
	private String combine(int[] value)
	{
		String result = "";
		for(int i=0; i<Constants.NUMBER_BALL+1; i++)
		{
			result += value[i]+"|";
		}
		result = result.substring(0, result.lastIndexOf("|"));
		return result.trim();
	}

	private int compareNums(int[] params)
	{
		int cnt = 0;
		int[] comein = new int[6];
		System.arraycopy(params, 0, comein, 0, comein.length);
		int supplIn = params[params.length-1];
		first : for(int i=0; i<comein.length; i++)
		{
			second : for(int j=0; j<latest.length; j++)
			{
				if(comein[i]==latest[j])
				{
					cnt++;
					break second;
				}
			}
		}
		return cnt;
	}
	
	private boolean compareSupplement(int[] params)
	{
		int supplIn = params[params.length-1];
		if(supplIn==supplement)
		{
			return true;
		}else{
			return false;
		}
	}
	
	private int[] getPopularity()
	{
		int[] result = null;
		try {
			result = cm.getPopularity();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	private int[] getResult(int draw)
	{
		int[] result = null;
		try {
			result = cm.getResult(draw);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	private int[] getResult()
	{
		int[] result = null;
		try {
			result = cm.getResult();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	private int getRecentDraw()
	{
		int draw = 0;
		try {
			draw = cm.getRecentDraw();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return draw;
		
	}
	
	@Override
	public void destroy() {
		super.destroy();
		if(cm!=null)
		{
			try {
				cm.disconnect();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
